package TopSky.API;

import java.io.IOException;
import java.sql.Timestamp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Hia.Utils.SkyResponse;
import TopSky.IOT.DAO.ControlDeviceDAO;
import TopSky.IOT.Models.controldevice;

/**
 * Servlet implementation class ControlDevice
 */
@WebServlet(name = "ControlDeviceInsert", urlPatterns = { "/ControlDeviceInsert" })
public class ControlDeviceInsert extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ControlDeviceInsert() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		controldevice model = new controldevice();
		SkyResponse skyresponse=new SkyResponse();
			
		String LabsID=request.getParameter("LabsID");
		if(LabsID!=null){
			model.setLabsID(Integer.parseInt(LabsID));
		}else{
			skyresponse.setErrorCode("labsID is null");
		}
		System.out.println(model.getLabsID());
		model.setName(request.getParameter("Name"));
		System.out.println(model.getName());
		model.setIntime(new Timestamp(System.currentTimeMillis()));
		model.setPicture(request.getParameter("Picture"));
		model.setInfo(request.getParameter("Info"));
		ControlDeviceDAO dao=new ControlDeviceDAO();
		dao.InsertOne(model);
		response.setCharacterEncoding("utf-8");
		//response.setHeader("Content-Encoding", "utf-8");
		response.setHeader("Content-Type", "text/html;charset=utf-8");
		response.getWriter().println("4");
		
		model.setSer(skyresponse);
	}
}
